package com.xinyues.gamemanager.service;

import java.util.Collection;
import java.util.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


@Service
public class CommonService {
    private Logger logger = LoggerFactory.getLogger(CommonService.class);

    public JSONObject response() {
        JSONObject result = new JSONObject();
        result.put("code", 0);
        return result;
    }
    public JSONObject response(Object value) {
        JSONObject result = new JSONObject();
        result.put("code", 0);
        result.put("msg", value);
        logger.debug("服务器返回数据:{}",result);
        return result;
    }
    public JSONObject responseTable(Object rows,int totalSize) {
        if(rows instanceof Collection || rows.getClass().isArray()) {
            JSONObject result = new JSONObject();
            result.put("total", totalSize);
            result.put("rows", rows);
            return result;
        }
        throw new IllegalArgumentException("参数必须是列表:" + JSON.toJSONString(rows));
        
    }
    public JSONObject responseEmptyTable() {
            JSONObject result = new JSONObject();
            result.put("total", Collections.emptyList());
            result.put("rows", 0);
            return result;
               
    }

    public JSONObject error(int code, String msg) {
        JSONObject result = new JSONObject();
        result.put("code", code);
        result.put("msg", msg);
        return result;
    }
}
